using System;
using System.IO;

namespace Logger_Handdler
{
    public class LogFile
    {
        #region WriteLoggerFile enum

        /// <summary>
        /// Write Logger File
        /// </summary>
        public enum WriteLoggerFile
        {
            Error,
            Info,
            Wrang,
            Unknown,
        } ;

        #endregion
        

         /*
            Create and Developer By Osama Khodrog
            email : khodrog@gmail.com
         */

        private const string LogPath = @"LogFile.log";

        /// <summary>
        /// Create And Write Logger File (Error Event)
        /// </summary>
        /// <param name="sEventType">Set event type (info , error) "string value from Enum"</param>
        /// <param name="sFormName">Set class name or form name "string value"</param>
        /// <param name="sTitleError">Set error title "stirng value" ex.Message</param>
        /// <param name="sErrorDescription">Set error description "string value" ex.stackTrace</param>
        public static void WriteLog(WriteLoggerFile sEventType, string sFormName, string sTitleError, string sErrorDescription)
        {
            StreamWriter objStreamWriter = null;
            try
            {
                if (!File.Exists(LogPath))
                {
                    File.Create(LogPath).Close();
                    objStreamWriter = File.AppendText(LogPath);
                    objStreamWriter.Write(DateTime.Now.ToLongDateString() + "  " +
                                          DateTime.Now.ToLongTimeString() + "[None][" + sEventType +
                                          "]Form Name:" + sFormName + "Error Title:" + sTitleError + "Error Description:" + sErrorDescription);
                }
                else
                {
                    objStreamWriter = File.AppendText(LogPath);
                    objStreamWriter.Write(DateTime.Now.ToLongDateString() + "  " +
                                          DateTime.Now.ToLongTimeString() + "[None][" + sEventType +
                                          "]Form Name:" + sFormName + "Error Title:" + sTitleError + "Error Description:" + sErrorDescription);
                }
            }
            finally
            {
                if (objStreamWriter != null)
                {
                    objStreamWriter.WriteLine();
                    objStreamWriter.Close();
                    objStreamWriter.Dispose();
                    GC.Collect();
                }
            }

        }

        /// <summary>
        /// Create And Write Logger File (Info Event)
        /// </summary>
        /// <param name="sEventRangeStatus">Set Event Range "Start , End" Just</param>
        /// <param name="sEventType">Set event type (info , error) "string value from Enum"</param>
        /// <param name="sFormName">Set class name or form name "string value"</param>
        public static void WriteLog(string sEventRangeStatus, WriteLoggerFile sEventType, string sFormName)
        {
            StreamWriter objStreamWriter = null;
            string sEventRange;

            if (sEventRangeStatus == "End")
            {
                sEventRange = "[End]  ";
            }
            else
            {
                sEventRange = "[" + sEventRangeStatus + "]";
            }

            try
            {
                if (!File.Exists(LogPath))
                {
                    File.Create(LogPath).Close();
                    objStreamWriter = File.AppendText(LogPath);
                    objStreamWriter.Write(DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString() +
                                          "[" + sEventType + "]" + "[None]" + sEventRange + "Form Name: " + sFormName);
                }
                else
                {
                    objStreamWriter = File.AppendText(LogPath);
                    objStreamWriter.Write(DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString() +
                                          "[" + sEventType + "]" + "[None]" + sEventRange + "Form Name: " + sFormName);
                }
            }
            finally
            {
                if (objStreamWriter != null)
                {
                    objStreamWriter.WriteLine();
                    objStreamWriter.Close();
                    objStreamWriter.Dispose();
                    GC.Collect();
                }
            }

        }
    }
}